<?php

namespace app\common\model;

class MemberLogs extends BaseModel
{
    protected $autoWriteTimestamp = true;

    public $name = 'member';


    //表字段别名
        public static $fieldsList = [
            'id'=>'ID',
            'username'=>'用户名',
            'headimg'=>'头像',
            'nickname'=>'昵称',
            'password'=>'密码',
            'role'=>'级别',
            'status'=>'状态',
            'create_time'=>'创建时间',

    ];

    //表字段状态
    public static $roleList = [
            '1'=>店主,
            '2'=>经理,
            '3'=>维修师傅,
    ];

    //级别获取器
    public function getRoleTextAttr($value,$data){
        if(strpos($data['role'],',')){
            $arr = explode(',',$data['role']);
            $arrText = [];
            foreach ($arr as $v){
                $arrText[]=self::$roleList[$v];
            }
            return join(',',$arrText);
        }else{
            return self::$roleList[$data['role']]?:'--';
        }
    }

    public static $statusList = [
            '1'=>开启,
            '0'=>关闭,
    ];

    //状态获取器
    public function getStatusTextAttr($value,$data){
        if(strpos($data['status'],',')){
            $arr = explode(',',$data['status']);
            $arrText = [];
            foreach ($arr as $v){
                $arrText[]=self::$statusList[$v];
            }
            return join(',',$arrText);
        }else{
            return self::$statusList[$data['status']]?:'--';
        }
    }



    //设置列表查询条件
    function getListWhere($model,$param=[]){
        if(empty($param)){
            return [];
        }
        $where = [];
            
        if($param['id']){
            $where['id'] = $param['id'];
        }
    
        if($param['username']){
            $where['username'] = $param['username'];
        }
    
        if($param['headimg']){
            $where['headimg'] = $param['headimg'];
        }
    
        if($param['nickname']){
            $where['nickname'] = $param['nickname'];
        }
    
        if($param['role']){
            $where['role'] = $param['role'];
        }
    
        if($param['status']){
            $where['status'] = $param['status'];
        }
        
        if( $param['create_time']!='' ) {
            $create_time = explode('至',$param['create_time']);
            $date_time = [
                strtotime($create_time[0]),
                strtotime($create_time[1].' 23:59:59'),
            ];
            $model->whereBetween('create_time',$date_time);
        }


//        //检索查询
        if($param['search_key']){
            $where['id'] = $param['search_key'];
        }
        if($where){
            $model->where($where);
        }
    }

        //数据查询
    function getList($param){
        $order = $param['order']?:'id desc';
        $model = $this->order($order);
        $this->getListWhere($model,$param);
        $list = $model->paginate($param['limit']);
        return $list;
    }

    //获取导出数据
    function getExport($where,$fileName='',$type='xlsx'){
        $fileName = $fileName?:'数据表格';
        $fileName.='-'.date('YmdHis');
        //获取数据
        $list = $this->where($where)->order('id desc')->select();
        if(empty($list)){
            return [];
        }
        foreach ($list as $k=>$v){
            $list[$k]['role'] = $v->role_text;
            $list[$k]['status'] = $v->status_text;

        }
        $list = $list->toArray();
        //得到表头
        $top = array_intersect_key(self::$fieldsList,$list[0]);
        return [
            'fileName'=>$fileName,
            'top'=>$top,
            'data'=>$list,
            'type'=>$type,
        ];
    }

}
